
import { extend } from '../tools/extend'
import { toArr } from './toArr'
import { inherits } from './inherits'
import { safeGet } from './safeGet'
import { isMiniProgram } from './isMiniProgram'
// 11937
/**
 * 创建 JavaScript 类
 * dependencies extend toArr inherits safeGet isMiniProgram
 * @param {*} methods Public methods
 * @param {*} statics Static methods
 */
export function Class (methods, statics) {
  
  var mx_extend = extend
  function makeClass (parent, methods, statics) {
    statics = statics || {};
    var className =
      methods.className || safeGet(methods, 'initialize.name') || '';
    delete methods.className;

    
    var _arguments = arguments
    var ctor = function () {
      var args = toArr(_arguments);
      console.log(_arguments)
      console.log(args)
      return this.initialize
        ? this.initialize.apply(this, args) || this
        : this;
    };
    if (!isMiniProgram) {
      // unsafe-eval CSP violation
      try {
        console.log("uuuuu ")
        ctor = new Function(
          'toArr',
          'return function ' +
          className +
          '()' +
          '{' +
          'var args = toArr(arguments);' +
          'return this.initialize ? this.initialize.apply(this, args) || this : this;' +
          '};'
        )(toArr);
      } catch (e) {
        /* eslint-disable no-empty */
      }
    }

    inherits(ctor, parent);
    ctor.prototype.constructor = ctor;

    ctor.extend = function (methods, statics) {
      return makeClass(ctor, methods, statics);
    };

    ctor.inherits = function (Class) {
      inherits(ctor, Class);
    };

    ctor.methods = function (methods) {
      extend(ctor.prototype, methods);
      return ctor;
    };

    ctor.statics = function (statics) {
      mx_extend(ctor, statics);
      return ctor;
    };

    ctor.methods(methods).statics(statics);
    console.log(ctor)
    return ctor;
  }

  var Base = (makeClass(Object, {
    className: 'Base',
    callSuper: function (parent, name, args) {
      var superMethod = parent.prototype[name];
      return superMethod.apply(this, args);
    },
    toString: function () {
      return this.constructor.name;
    }
  }));
  console.log("hall")
  return Base.extend(methods, statics);

}